package com.xxx.services.impl;

import com.xxx.common.AppResult;
import com.xxx.common.ResultCode;
import com.xxx.dao.BoardMapper;
import com.xxx.exception.ApplicationException;
import com.xxx.model.Board;
import com.xxx.services.IBoardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Slf4j
@Service
public class BoardServiceImpl implements IBoardService {
    @Autowired
    private BoardMapper boardMapper;
    @Override
    public List<Board> selectByNum(Integer num) {
        //1. 非空校验
        if(num <= 0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //2. 调用dao 查询数据库
        return boardMapper.selectByNum(num);
    }

    @Override
    public void addOneArticleCountById(Long id) {
        if(id == null || id <= 0){
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }

        //查询对应的板块
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board == null){
            log.info(ResultCode.ERROR_IS_NULL.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        //更新帖子数量
        Board updateBoard = new Board();//重新创建一个用来更新的类
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount() + 1);

        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if(row != 1){
            log.warn(ResultCode.FAILED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public Board selectById(Long id) {
        if(id == null || id == 0){
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        Board board = boardMapper.selectByPrimaryKey(id);
        return board;
    }

    @Override
    public void subOneArticleById(Long id) {
        if(id == null || id <= 0){
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }

        //查询对应的板块
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board == null){
            log.info(ResultCode.ERROR_IS_NULL.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        //更新帖子数量
        Board updateBoard = new Board();//重新创建一个用来更新的类
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount() - 1);
        //判断- 1之后是否<0
        if(updateBoard.getArticleCount() < 0){
            //< 0 设置为 0
            updateBoard.setArticleCount(0);
        }

        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if(row != 1){
            log.warn(ResultCode.FAILED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }
}
